package com.jrx.anytxn.param.job.investor.investorLimit.config;

import com.jrx.anytxn.param.config.AnytxnBatchProperties;
import com.jrx.anytxn.param.entity.PrInvestorLimit;
import com.jrx.anytxn.param.job.investor.investorLimit.step.InvestorLimitProcessor;
import com.jrx.anytxn.param.job.investor.investorLimit.step.InvestorLimitWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;

/**
 * 出资方额度更新
 *
 * @author liuyang
 * @date 2020-06-01
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class InvestorLimitConfiguration {
    @Resource(name = "paramSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private InvestorLimitProcessor investorLimitProcessor;

    @Autowired
    private InvestorLimitWriter investorLimitWriter;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job investorLimitJob(){
        return jobBuilderFactory.get("investorLimitJob").start(investorLimitStep()).build();
    }

    /**
     * 出资方额度更新流程处理
     *
     * @return Step
     * @author liuyang
     * @date 2020-06-01
     */
    @Bean
    public Step investorLimitStep(){
        return stepBuilderFactory.get("investorLimitStep")
                .<PrInvestorLimit, PrInvestorLimit>chunk(anytxnBatchProperties.getInvestorLimitConfiguration().getChunkLimit())//表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(investorLimitReader())
                .processor(investorLimitProcessor)
                .writer(investorLimitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getInvestorLimitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    /**
     * 查询出资方额度定义
     *
     * @return SynchronizedItemStreamReader<PrInvestorLimit> 出资方额度定义
     * @author liuyang
     * @date 2020-06-01
     */
    @Bean
    @StepScope
    public SynchronizedItemStreamReader<PrInvestorLimit> investorLimitReader() {

        MyBatisCursorItemReader<PrInvestorLimit> orgCursorItemReader = new MyBatisCursorItemReader<>();
        orgCursorItemReader.setSaveState(false);
        orgCursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        //查询方法
        orgCursorItemReader.setQueryId("com.jrx.anytxn.param.mapper.ext.ExtPrInvestorLimitMapper" +
                ".selectAllInvestorLimit");
        //同步reader   线程安全
        SynchronizedItemStreamReader<PrInvestorLimit> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(orgCursorItemReader);
        return synchronizedItemStreamReader;
    }
}
